<?php
	class ModelCatalogTFilter extends Model
	{
		public function getProducts($filter)
		{
			$db_prefix=DB_PREFIX;
			
			$settings_ids=$this->makeFilterSettings($filter["settings"]);
			$filter_settings="`p`.`product_id` IN ({$settings_ids})";

			$item_lists=array();
			
			if(isset($filter["ag"]) && $filter["ag"] && !empty($filter["ag"]))
			{
				foreach($filter["ag"] as $key=>$ag)
				{
					$ag=$ag["value"];
					$ag=(isset($ag["value"]) && $ag["value"])?$ag["value"]:'tfmall';
					$tmp_list=array();
					if(is_array($ag))
					{
						if(in_array("tfmall",$ag) || empty($ag))
						{
							continue;
						}
						else
						{
							$ag_opts=array();
							foreach($ag as $agi)
							{
								$ag_opts[]="(`a`.`attribute_group_id`='{$key}' AND `pa`.`text`='{$agi}')";
							}
							$ag_opts="(".implode(" OR ",$ag_opts).")";
							$result=$this->db->query("SELECT `pa`.`product_id` FROM `".DB_PREFIX."product_attribute` AS pa INNER JOIN `{$db_prefix}attribute_group` AS ag ON `a`.`attribute_id`=`pa`.`attribute_id`  WHERE {$ag_opts}");
							
							foreach($result->rows as $row)
							{
								$tmp_list[]=$row["product_id"];
							}
						}
					}
					else
					{
						if($ag=='tfmall')
						{
							continue;
						}
						$result=$this->db->query("SELECT `pa`.`product_id` FROM `".DB_PREFIX."product_attribute` AS pa INNER JOIN `{$db_prefix}attribute` AS a ON `a`.`attribute_id`=`pa`.`attribute_id` WHERE `a`.`attribute_group_id`='{$key}' AND `pa`.`text`='{$ag}'");
						
						foreach($result->rows as $row)
						{
							$tmp_list[]=$row["product_id"];
						}
					}
					
					if(empty($tmp_list))
					{
						continue;
					}
					$item_lists[]=$tmp_list;
				}
				
			}
			
			if(isset($filter["a"]) && $filter["a"] && !empty($filter["a"]))
			{
				foreach($filter["a"] as $key=>$a)
				{
					$a=(isset($a["value"]) && $a["value"])?$a["value"]:'tfmall';
					$tmp_list=array();
					if(is_array($a))
					{
						if(in_array("tfmall",$a) || empty($a))
						{
							continue;
						}
						else
						{
							$a_opts=array();
							foreach($a as $ai)
							{
								$a_opts[]="(`attribute_id`='{$key}' AND `text`='{$ai}')";
							}
							$a_opts="(".implode(" OR ",$a_opts).")";
							$result=$this->db->query("SELECT `product_id` FROM `".DB_PREFIX."product_attribute` WHERE {$a_opts}");
							
							foreach($result->rows as $row)
							{
								$tmp_list[]=$row["product_id"];
							}
						}
					}
					else
					{
						if($a=='tfmall')
						{
							continue;
						}
						$result=$this->db->query("SELECT `product_id` FROM `".DB_PREFIX."product_attribute` WHERE `attribute_id`='{$key}' AND `text`='{$a}'");
						
						foreach($result->rows as $row)
						{
							$tmp_list[]=$row["product_id"];
						}
					}
					
					if(empty($tmp_list))
					{
						continue;
					}
					$item_lists[]=$tmp_list;
				}
				
			}
			
			if(isset($filter["pf"]) && $filter["pf"] && !empty($filter["pf"]))
			{
				foreach($filter["pf"] as $key=>$pf)
				{
					$range=$pf["range"];
					$pf=(isset($pf["value"]) && $pf["value"])?$pf["value"]:'tfmall';
					$tmp_list=array();
					if($key=='category')
					{
						if(is_array($pf))
						{
							if(in_array("tfmall",$pf) || empty($pf))
							{
								continue;
							}
							
							$pf=implode(",",$pf);
							$pf=array_unique(explode(",",$pf));
							$pf=implode(",",$pf);
						}
						elseif($pf=='tfmall')
						{
							continue;
						}
						
						$result=$this->db->query("SELECT DISTINCT `p`.`product_id` FROM `".DB_PREFIX."product` AS p LEFT JOIN `".DB_PREFIX."product_to_category` AS p2c ON `p`.`product_id`=`p2c`.`product_id`  WHERE `p2c`.`category_id` IN ({$pf})");
						
						foreach($result->rows as $row)
						{
							$tmp_list[]=$row["product_id"];
						}
					}
					else
					{
						if(is_array($pf))
						{
							if(in_array("tfmall",$pf) || empty($pf))
							{
								continue;
							}
							else
							{
								$pf_opts=array();
								foreach($pf as $pfi)
								{
									if($range)
									{
										list($min,$max)=explode(" - ",$pf);
										$pf_opts[]="(`{$key}`<='{$max}' AND `{$key}`>='{$min}')";
									}
									else
									{
										$pf_opts[]="(`{$key}`='{$pfi}')";
									}
								}
								$pf_opts="(".implode(" OR ",$pf_opts).")";
								$result=$this->db->query("SELECT `product_id` FROM `".DB_PREFIX."product` WHERE {$pf_opts}");
								
								foreach($result->rows as $row)
								{
									$tmp_list[]=$row["product_id"];
								}
							}
						}
						else
						{
							if($pf=='tfmall')
							{
								continue;
							}
							if($range)
							{
								list($min,$max)=explode(" - ",$pf);
								$result=$this->db->query("SELECT `product_id` FROM `".DB_PREFIX."product` WHERE `{$key}`<='{$max}' AND `{$key}`>='{$min}'");
							}
							else
							{
								$result=$this->db->query("SELECT `product_id` FROM `".DB_PREFIX."product` WHERE `{$key}`='{$pf}'");
							}
							
							foreach($result->rows as $row)
							{
								$tmp_list[]=$row["product_id"];
							}
						}
					}
					
					if(empty($tmp_list))
					{
						continue;
					}
					
					$item_lists[]=$tmp_list;
					
				}
			}
			
			if(empty($item_lists))
			{
				$item_opts="TRUE";
			}
			else
			{
				$pid_list=$item_lists[0];
				foreach($item_lists as $list)
				{
					$pid_list=array_intersect($pid_list,$list);
				}
				$pid_list=implode(",",$pid_list);
				$item_opts="`p`.`product_id` IN (".(($pid_list)?$pid_list:"0").")";
			}
			$std="pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
			$q=<<<SQL
			SELECT DISTINCT `p`.`product_id` FROM `{$db_prefix}product` AS p LEFT JOIN `{$db_prefix}product_attribute` AS pa ON `pa`.`product_id`=`p`.`product_id` LEFT JOIN `{$db_prefix}product_description` AS pd ON `pd`.`product_id`=`p`.`product_id` LEFT JOIN `{$db_prefix}product_to_store` AS `p2s` ON `p2s`.`product_id`=`p`.`product_id` WHERE ({$item_opts}) AND ({$filter_settings}) AND ($std)
SQL;
			$list=array();
			$result=$this->db->query($q);
			foreach($result->rows as $res)
			{
				$list[]=$res["product_id"];
			}
			$count=$this->db->countAffected();
			return array("total"=>$count,"list"=>$list);
		}
		
		private function makeFilterSettings($settings)
	{
		switch($settings["range_type"])
		{
			case "a":$cond="`pa`.`attribute_id`='{$settings["range_id"]}'";break;
			case "ag":$cond="`pa`.`attribute_id` IN (SELECT `attribute_id` FROM `".DB_PREFIX."attribute` AS a WHERE `attribute_group_id`='{$settings["range_id"]}')";break;
			case "c":$cond="`p2c`.`category_id`='{$settings["range_id"]}'";break;
			case "m":$cond="`p`.`manufacturer`='{$settings["range_id"]}'";break;
			default:$cond="TRUE";
		}
		
		if($settings["instock"])
		{
			$cond="({$cond}) AND (`p`.`stock_status_id` IN ({$settings["instock"]}))";
		}
		
		$q="SELECT DISTINCT `p`.`product_id` FROM `".DB_PREFIX."product` AS p LEFT JOIN `".DB_PREFIX."product_attribute` AS pa ON `pa`.`product_id`=`p`.`product_id` LEFT JOIN `".DB_PREFIX."product_to_category` AS p2c ON `p2c`.`product_id`=`p`.`product_id` WHERE {$cond}";
		
		$ids="0";
		
		foreach($this->db->query($q)->rows as $row)
		{
			$ids.=",".$row["product_id"];
		}
		return $ids;
	}
	}
?>